<?php


class System
{
    public static function valid_youtube_url($url)
    {

        $matches = preg_match("/http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/", $url);
        if ((bool)$matches) {
            return true;
        } else {
            return false;
        }

    }
    
    public static function parse_youtube_url($url,$return='embed',$width='',$height='',$rel=0){
        $urls = parse_url($url);

        //url is http://youtu.be/xxxx
        if($urls['host'] == 'youtu.be'){
            $id = ltrim($urls['path'],'/');
        }
        //url is http://www.youtube.com/embed/xxxx
        else if(strpos($urls['path'],'embed') == 1){
            $id = end(explode('/',$urls['path']));
        }
         //url is xxxx only
        else if(strpos($url,'/')===false){
            $id = $url;
        }
        //http://www.youtube.com/watch?feature=player_embedded&v=m-t4pcO99gI
        //url is http://www.youtube.com/watch?v=xxxx
        else{
            parse_str($urls['query']);
            $id = $v;
            if(!empty($feature)){
                $id = end(explode('v=',$urls['query']));
            }
        }
        //return embed iframe
        if($return == 'embed'){
            return '<iframe width="'.($width?$width:560).'" height="'.($height?$height:349).'" src="http://www.youtube.com/embed/'.$id.'?rel='.$rel.'" frameborder="0" allowfullscreen></iframe>';
        }
        //return normal thumb
        else if($return == 'thumb'){
            return 'http://i1.ytimg.com/vi/'.$id.'/default.jpg';
        }
        //return hqthumb
        else if($return == 'hqthumb'){
            return 'http://i1.ytimg.com/vi/'.$id.'/hqdefault.jpg';
        }
        // else return id
        else{
            return $id;
        }
    }

    public static function EncodeUrl($url)
    {
            
        $url = substr($url, 10);
        
        $params = explode("&", $url);
        $result = "";
        
        $i=0;
        foreach ($params as $param)
        {
            
            $s = explode("=", $param);
            if ($i<2) 
                $result .= $s[1]."/";
            else
                $result .= $s[0]."/".$s[1]."/";
            
            $i++;
        }
        
        echo "./".$result;
        
    }
    
    public static function run($_controller, $_action)
    {
       
        if (class_exists($_controller, false) || interface_exists($_controller, false))
        {
            if (trim($_action)=='') $_action = 'index';
                        
            if (method_exists($_controller,$_action))
            {
                 $data=array(
                     'controller' => $_controller,
                     'action' => $_action
                 );
                 $config = Controller::getConfig();
                 if ($config['global']['logUserHistory']) {
                    Log::saveToHistory($data);
                 }
                 $_controller = new $_controller();
                 $_controller->pre();     
                 $_controller->$_action();                 
                 View::template($_controller->getTemplate()); 
                 View::content($_controller->getContentFile());
                 require_once View::render($_controller->getLayout()); 
                 $_controller->post();
            }
        }
        else
            die('Sorry, this URL is not valid');
    }
    
    public static function get_include_contents($filename) {
        if (is_file($filename)) {
            ob_start();
            include $filename;
            return ob_get_clean();
        }
        return false;
    }
    
    static function isValidEmail($email){
      
        if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) {
            return false;
        }
        return true;

    }
    
    static function getRealIpAddr()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
        {
          $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
        {
          $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
    
    public static function redirect($url, $status = '')
    {
        switch($status) {
            case '404':
                header("HTTP/1.1 404 Not Found");
                header('Location: ' . $url);
                die();
                break;
            case '301':
                header("HTTP/1.1 301 Moved Permanently");
                header('Location: ' . $url);
                die();
                break;
            default:
                header('Location: ' . $url);
                die();
                exit;
        }    
    }
    
    public static function getmicrotime()
    {
          list($usec, $sec) = explode(" ",microtime());
          return ((float)$usec + (float)$sec);       
    }
    
    public static function __autoload($classNameArray)
    {
        foreach($classNameArray as $classname) 
        {
            try {
                if (class_exists($classname, false) || interface_exists($classname, false)){
                    return;
                }
                $class = explode('_', $classname);

                $deeps = count($class);

                $type = $class[1];
                $name = $class[0];

                switch($type)
                {
                    case 'Controller': $path = Path::getControllerPath(); $filename=$name."Controller"; break;
                    default: die("Error. Bad class name.");
                }

                $filename = basename($filename);

                $file = $path.$filename.'.php';

                if (!file_exists($file) || !is_readable($file) || class_exists($classname, false) || interface_exists($classname, false)){
                    return false;
                }else{
                    require_once($file);
                    return true;
                }
            } catch (Exception $e){
                return false;
            }
        }
    }
}

?>
